package com.nut.controller;

import com.nut.entity.Admin;
import com.nut.service.AdminService;
import com.nut.service.UserService;
import com.nut.vo.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @ author : HyoJoo-W
 * @ createDate : 2021-04-22 20:21
 * @ Description : 管理员Controller
 */
@Controller
@CrossOrigin
@RequestMapping("/admin")
public class AdminController
{
    @Autowired
    private AdminService adminService;

    @ResponseBody
    @RequestMapping("/login")
    public Message login(@RequestParam("name")String name,@RequestParam("password")String password)
    {
        Admin admin = new Admin();
        admin.setName(name);
        admin.setPassword(password);
        admin = adminService.login(admin);
        if(admin!=null)
        {
            return Message.success().payLoad("admin",admin);
        }
        else
        {
            return Message.fail();
        }
    }

    @ResponseBody
    @RequestMapping("/register")
    public Message register(@RequestParam("name")String name,@RequestParam("password")String password)
    {
        Admin admin = new Admin();
        admin.setName(name);
        admin.setPassword(password);

        try
        {
            adminService.registerAdmin(admin);
            return Message.success().payLoad("message","注册成功！");
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return Message.fail().payLoad("message","注册失败！");
        }
    }
}
